import { loginApi } from "@/api/user"
import { getToken, setToken, removeToken } from "@/utils/auth"
// 状态
const state = {
  token: getToken()
}
// 修改状态
const mutations = {
  // 设置token
  setToken ( state, data ) {
    console.log( data );
    state.token = data
    setToken( data )
  },
  // 删除缓存
  removeToken ( state ) {
    state.token = null // 删除vuex的token
    removeToken() // 先清除 vuex  再清除缓存 vuex和 缓存数据的同步
  }
}
// 执行异步
const actions = {
  async loginAction ( context, data ) {
    let res = await loginApi( data )
    console.log( res );
    context.commit( "setToken", res )
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}